package Queue;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @Description:优先队列的演示以及基本使用
 * @Author:AMark
 * @Date: 11/1/2023
 */

//自定义类
class cat{
    public String name;
    public int age;
    cat(String name, int age){
        this.age = age;
        this.name = name;
    }
}
public class PriorityQueueDemo {

    //使用匿名类的创建方法
    static Comparator<cat> catComparator = new Comparator<cat>() {
        @Override
        public int compare(cat o1, cat o2) {
            return o1.age-o2.age;
        }
    };
    //使用优先队列实现排序
    public static void main(String[] args) {
        Queue<cat> catQueue = new PriorityQueue<>(catComparator);
        cat c1 = new cat("a",1);
        cat c2 = new cat("b",2);
        cat c3 = new cat("c",3);
        cat c4 = new cat("d",4);
        cat c5 = new cat("f",5);
        cat c6 = new cat("g",6);
        catQueue.add(c1);
        catQueue.add(c2);
        catQueue.add(c3);
        catQueue.add(c4);
        catQueue.add(c5);
        catQueue.add(c6);
        cat temp;
        while (!catQueue.isEmpty()) {
            temp = catQueue.poll();//返回头部元素并将头部删除
            System.out.println("名字："+temp.name);
        }
    }
}
